package com.wzq.upms.core;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.wzq.permission.client.model.admin.MenuModel;

/**
 * 权限控制自定义标签
 * Project:wzq-upms 
 * @Author chenjianfei
 * create Time:2016年2月22日  8:38 
 *
 */
public class PermissionTag extends TagSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8298125397448279046L;
	private String url;
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
		List<MenuModel> permission = SessionUtils.getUserPermissionList(request);
		boolean show = false;
		if(permission != null && StringUtils.isNotEmpty(url)){
			for(MenuModel menuModel:permission){
				 String linkUrl = menuModel.getLinkUrl();
				 if(linkUrl!=null && !"".equals(linkUrl)){
					 String[] menuUrls = linkUrl.split("\\|");
					 for(String str:menuUrls){
						 if(url.contains(str)){
							 show = true;
							 break;
						 }
					 }
					 if(show){
						 break;
					 }
				 }
			}
		}
		if (show) {
            return TagSupport.EVAL_BODY_INCLUDE;
        } else {
            return TagSupport.SKIP_BODY;
        }
	}
	
}
